package com.limingru.demoproject.controller;

import com.limingru.demoproject.domain.User;
import com.limingru.demoproject.service.UserService;
import com.limingru.demoproject.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/v1/pub/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("login")
    public JsonData login(@RequestBody User user) {
        String token = userService.login(user.getUsername(), user.getPwd());
        return token != null ? JsonData.buildSuccess(token) : JsonData.buildError(-1, "账号密码错误");
    }

    @GetMapping("list")
    public JsonData listUser() {
        List<User> users = userService.listUser();
        return JsonData.buildSuccess(users);
    }
}
